//
//  MOMyGetMarriedAddPeopleView.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/19.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOMyGetMarriedAddPeopleView.h"
#import "MOMyGetMarriedProjectEditOrCreatViewCell.h"

@interface MOMyGetMarriedAddPeopleView ()<UITableViewDelegate,UITableViewDataSource,XLsn0wPickerTimerDelegate>

@property(nonatomic,strong)UIView *backView;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation MOMyGetMarriedAddPeopleView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self) {
        
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7];
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [button addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        [self addSubview:self.backView];
        
        [self initData];
        
    }
    return self;
}



#pragma mark -- 取消
-(void)clickCancelButton
{
    self.frame = CGRectMake(0, 0, 0, 0);
    self.backView.frame = CGRectMake(0, 0, 0, 0);
}

#pragma mark -- 确定
-(void)clickSubmitButton
{
    for (int i = 0; i<self.dataSource.count; i++) {
        MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[i];
        if (model.valueStr.length==0) {
            NSString *str = @"";
            switch (i) {
                case 0:
                    str = @"请填写户主姓名";
                    break;
                case 1:
                    str = @"请选择户主关系";
                    break;
                case 2:
                    str = @"请选择性别";
                    break;
                case 3:
                    str = @"请选择出生年月";
                    break;
                case 4:
                    str = @"请选择文化程度";
                    break;
                case 5:
                    str = @"请选择政治面貌";
                    break;
                case 6:
                    str = @"请填写职业";
                    break;
                case 7:
                    str = @"请选择健康状况";
                    break;
                
                default:
                    break;
            }
            [[ADShowMessage shareShowMessage]showTextOnly:str showInView:self];
            return;
        }
    }
    
    self.frame = CGRectMake(0, 0, 0, 0);
    self.backView.frame = CGRectMake(0, 0, 0, 0);
    
    if (self.clickSureButtonBlock) {
        self.clickSureButtonBlock(self.dataSource);
    }
}

-(void)initData
{
    NSArray *arr = @[@"姓名",@"户主关系",@"性别",@"出生年月",@"文化程度",@"政治面貌",@"职业",@"健康状况"];
    NSArray *pArr = @[@"请填写户主姓名",@"请选择户主关系",@"请选择性别",@"请选择出生年月",@"请选择文化程度",@"请选择政治面貌",@"请填写职业",@"请选择健康状况"];
    self.dataSource = [NSMutableArray array];
    for (int i = 0; i<arr.count; i++) {
        MOMyGetMarriedProjectEditOrCreatViewModel *model = [[MOMyGetMarriedProjectEditOrCreatViewModel alloc]init];
        model.nameStr = arr[i];
        model.placeStr = pArr[i];
        model.labelWidth = 100;
        model.KeyboardType = 0;
        model.cellWidth = _backView.width;
        if (i==1||i==2||i==3||i==4||i==5||i==7) {
            model.layoutType = @"3";
        }else{
            model.layoutType = @"1";
        }
        
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
}

-(void)setEndModel:(MOMyGetMarriedAddPeopleModel *)endModel
{
    _endModel = endModel;
}

-(void)setEditOrCreatStatus:(NSString *)editOrCreatStatus
{
    _editOrCreatStatus = editOrCreatStatus;
    if ([editOrCreatStatus integerValue]==1) {
        for (int i = 0; i<self.dataSource.count ; i++) {
            
            MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[i];
            
            if ([self.editOrCreatStatus integerValue]==1) {
                switch (i) {
                    case 0:
                        model.valueStr = self.endModel.xingming;
                        break;
                    case 1:
                        model.valueStr = self.endModel.shenfen;
                        break;
                    case 2:
                        model.valueStr = self.endModel.xingbie;
                        break;
                    case 3:
                        model.valueStr = self.endModel.chushengnianyue;
                        break;
                    case 4:
                        model.valueStr = self.endModel.wenhuachengdu;
                        break;
                    case 5:
                        model.valueStr = self.endModel.zhengzhimianmao;
                        break;
                    case 6:
                        model.valueStr = self.endModel.zhiye;
                        break;
                    case 7:
                        model.valueStr = self.endModel.jiamkangzhuangkuang;
                        break;
                        
                    default:
                        break;
                }
            }
        }
        [self.tableView reloadData];
    }
}


#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOMyGetMarriedProjectEditOrCreatViewCell *cell = [MOMyGetMarriedProjectEditOrCreatViewCell cellWithTableView:tableView andIndexPath:indexPath];
    MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[indexPath.row];
    cell.textFieldEndEditingBlock = ^(NSString * _Nonnull text) {
        model.valueStr = text;
    };
    cell.model = model;
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 13;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //if (i==1||i==2||i==3||i==4||i==5||i==7) {
    MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[indexPath.row];
    if (indexPath.row==1) {
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"配偶",@"父母",@"子女",@"兄弟姐妹"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            model.valueStr = tArr[index];
            [weakSelf.tableView reloadData];
        };
        [WINDOW addSubview:pickview];
    }else if (indexPath.row==2){
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"男",@"女"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            model.valueStr = tArr[index];
            [weakSelf.tableView reloadData];
        };
        [WINDOW addSubview:pickview];
    }else if (indexPath.row==3){
        XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
        pickerDate.maxComponentCount = 3;
        pickerDate.xlsn0wDelegate = self;
        [pickerDate show];
    }else if (indexPath.row==4){
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"小学",@"初中",@"高中",@"大学",@"研究生"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            model.valueStr = tArr[index];
            [weakSelf.tableView reloadData];
        };
        [WINDOW addSubview:pickview];
    }else if (indexPath.row==5){
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"党员",@"团员",@"群众"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            model.valueStr = tArr[index];
            [weakSelf.tableView reloadData];
        };
        [WINDOW addSubview:pickview];
    }else if (indexPath.row==7){
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"良好",@"一般",@"较差"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            model.valueStr = tArr[index];
            [weakSelf.tableView reloadData];
        };
        [WINDOW addSubview:pickview];
    }
}

- (void)pickerTimer:(XLsn0wPickerTimer *)pickerTimer year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    
    MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[3];
    NSString *dateStr = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)year, (long)month, (long)day];
    model.valueStr = dateStr;
    [self.tableView reloadData];
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -- 懒加载
-(UIView *)backView
{
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - kWConversions(55), kHConversions(400))];
        _backView.centerX = SCREEN_WIDTH/2;
        _backView.centerY = SCREEN_HEIGHT/2;
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = 6.0;
        
        [_backView addSubview:self.tableView];
        
        UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(0,self.tableView.bottom, self.tableView.width, kHConversions(54))];
        [submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [submitButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        submitButton.titleLabel.font = FONT_SIZE(15);
        [submitButton addTarget:self action:@selector(clickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [_backView addSubview:submitButton];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _backView.width, 1)];
        lineView.backgroundColor = KGrayColor_ededed;
        [submitButton addSubview:lineView];
    }
    return _backView;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, _backView.width, _backView.height - kHConversions(54)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}


@end
